package org.dpes.compare;

import java.io.File;


public abstract class AComparator {

	
	/**
	 * <code>NoType</code> - means... 
	 * <br><code>MD5Cmp</code> - means... 
	 * <br><code>FullCmp</code> - means... 
	 */
	public enum CmpType {NoType, MD5Cmp, FullCmp};
	
	public static CmpResult compareFiles(File left, File right, CmpType type) {
		
		CmpResult result = null;
		
		switch (type) {
		
		case MD5Cmp: result = new MD5Comparator().beforeCompare(left, right);
			break;
		
		case FullCmp: result = new FullComparator().beforeCompare(left, right);
			break;

		default:
			break;
		}
		
		return result;
	}
	
	CmpResult beforeCompare(File left, File right){
		if (left == null || right == null)
			return new CmpResult(false);
		
		if (left.hashCode() == right.hashCode())
			return new CmpResult(true);
		
		if (! left.exists() || ! right.exists())
			return new CmpResult(false);
		
		if (isFilesTheSame(left, right))
			return new CmpResult(true);
		
		return compare(left, right);
	}
	
	abstract CmpResult compare(File left, File right);
	
	private boolean isFilesTheSame(File left, File right) {
		if (left.compareTo(right) == 0)
			return true;
		
		return false;
	}
}
